import path from 'path'
import LineByline from 'n-readlines'
import analytics from './analytics'

const filepath = path.join(__dirname, './test/newanalytics.json')
const liner = new LineByline(filepath)
const temp = []
let line = true
let total = 0

const format = function(_line) {
    let _data = _line.toString()
    _data = JSON.parse(_data)
    delete _data._id
    delete _data._snid
    delete _data.__v
    _data.z = _data.z.$date
    _data.a4 = _data.a4.$date
    return _data
}

const foo = async function() {
    console.log('正在执行插入数据......')
    try {
        console.time('Insert')
        while (line) {
            line = liner.next()
            if (line === false) {
                break
            }
            const _data = format(line) 
            total +=1
            if (!line) break
            temp.push(_data)
            if (temp.length === 500) {
                await analytics.create(temp)
                temp.length = 0
            }
        }

        if (temp.length > 0) {
            await analytics.create(temp)
        }
        console.timeEnd('Insert')
        console.log(`执行完毕 共${ total }条`)
    } catch (error) {
         console.log(e)
    }
}

foo()